import { apiWrapper } from '@/utils/request'

// ========================= 用户API (/User) =========================
export const userApi = {
  // 用户登录
  login: (data) => apiWrapper.post('/User/user/login', data),

  // 用户注册
  register: (data) => apiWrapper.post('/User/user/register', data),

  // 获取用户信息
  getUserInfo: () => apiWrapper.get('/User/user/userInfo'),

  // 更新用户个人资料
  updateProfile: (data) => apiWrapper.put('/User/user/updateProfile', data),

  // 获取用户银行卡列表
  getUserCards: () => apiWrapper.get('/User/user/cards'),

  // 分页查询用户交易记录
  pageTradeUser: (data, begin, end) => {
    const params = new URLSearchParams()
    if (begin) params.append('begin', begin)
    if (end) params.append('end', end)
    const url = `/User/pageTradeUser${params.toString() ? '?' + params.toString() : ''}`
    return apiWrapper.post(url, data)
  },

  // 上传头像
  uploadImage: (file) => {
    const formData = new FormData()
    formData.append('file', file)
    return apiWrapper.post('/User/uploadImage', formData, {
      headers: { 'Content-Type': 'multipart/form-data' },
    })
  },
}

// ========================= 银行卡操作API (/User/cards) =========================
export const cardApi = {
  // 存款
  deposit: (data) => apiWrapper.post('/User/cards/deposit', data),

  // 取款
  withdraw: (data) => apiWrapper.post('/User/cards/withdraw', data),

  // 转账
  transfer: (data) => apiWrapper.post('/User/cards/transfer', data),

  // 查询银行卡余额
  getBalance: (cardId) => apiWrapper.get(`/User/cards/${cardId}`),

  // 挂失银行卡
  reportLoss: (data) => apiWrapper.put('/User/cards/reportLoss', data),

  // 修改银行卡密码
  changePassword: (data) => apiWrapper.put('/User/cards/changedPwd', data),
}

// ========================= 管理员客户管理API (/Admin/customer) =========================
export const adminCustomerApi = {
  // 管理员登录
  login: (data) => apiWrapper.post('/Admin/customer/login', data),

  // 获取管理员信息
  getAdminInfo: () => apiWrapper.get('/Admin/customer/getAdminInfo'),

  // 新增客户
  addCustomer: (data) => apiWrapper.post('/Admin/customer/addCustomer', data),

  // 更新客户信息
  updateCustomer: (data) => apiWrapper.put('/Admin/customer/updateCustomer', data),

  // 删除客户
  deleteCustomer: (id) => apiWrapper.delete(`/Admin/customer/deleteCustomer/${id}`),

  // 分页查询客户
  pageCustomer: (data, begin, end) => {
    const params = new URLSearchParams()
    if (begin) params.append('begin', begin)
    if (end) params.append('end', end)
    const url = `/Admin/customer/pageCustomer${params.toString() ? '?' + params.toString() : ''}`
    return apiWrapper.post(url, data)
  },

  // 上传图片
  uploadImage: (file) => {
    const formData = new FormData()
    formData.append('file', file)
    return apiWrapper.post('/Admin/customer/uploadImage', formData, {
      headers: { 'Content-Type': 'multipart/form-data' },
    })
  },
}

// ========================= 管理员银行卡管理API (/Admin/card) =========================
export const adminCardApi = {
  // 新增银行卡
  addCard: (data) => apiWrapper.post('/Admin/card/addCard', data),

  // 修改银行卡密码
  changePwd: (data) => apiWrapper.put('/Admin/card/changePwd', data),

  // 获取银行卡状态
  getStatus: (cardid) => apiWrapper.get(`/Admin/card/getStatus/${cardid}`),

  // 修改银行卡状态
  changeStatus: (data) => apiWrapper.put('/Admin/card/changeStatus', data),

  // 删除银行卡
  deleteCard: (cardid) => apiWrapper.delete(`/Admin/card/deleteCard/${cardid}`),

  // 根据银行卡号获取银行卡信息
  getCard: (cardid) => apiWrapper.get(`/Admin/card/getCard/${cardid}`),

  // 根据身份证获取银行卡列表
  getCardsByPid: (pid) => apiWrapper.get(`/Admin/card/getCardsByPid/${pid}`),

  // 分页查询银行卡
  pageCard: (data, begin, end) => {
    const params = new URLSearchParams()
    if (begin) params.append('begin', begin)
    if (end) params.append('end', end)
    const url = `/Admin/card/pageCard${params.toString() ? '?' + params.toString() : ''}`
    return apiWrapper.post(url, data)
  },
}

// ========================= 管理员交易管理API (/Admin/trade) =========================
export const adminTradeApi = {
  // 删除交易记录
  deleteTrade: (tradeid) => apiWrapper.delete(`/Admin/trade/deleteTrade/${tradeid}`),

  // 分页查询交易记录
  pageTrade: (data, begin, end) => {
    const params = new URLSearchParams()
    if (begin) params.append('begin', begin)
    if (end) params.append('end', end)
    const url = `/Admin/trade/pageTrade${params.toString() ? '?' + params.toString() : ''}`
    return apiWrapper.post(url, data)
  },
}

// ========================= 管理员存款类型管理API (/Admin/deposit) =========================
export const adminDepositApi = {
  // 新增存款类型
  addDeposit: (data) => apiWrapper.post('/Admin/deposit/addDeposit', data),

  // 更新存款类型
  updateDeposit: (data) => apiWrapper.put('/Admin/deposit/updateDeposit', data),

  // 删除存款类型
  deleteDeposit: (id) => apiWrapper.delete(`/Admin/deposit/deleteDeposit/${id}`),

  // 获取存款类型信息
  getDeposit: (savingid) => apiWrapper.get(`/Admin/deposit/getDeposit/${savingid}`),

  // 分页查询存款类型
  pageDeposit: (data, begin, end) => {
    const params = new URLSearchParams()
    if (begin) params.append('begin', begin)
    if (end) params.append('end', end)
    const url = `/Admin/deposit/pageDeposit${params.toString() ? '?' + params.toString() : ''}`
    return apiWrapper.post(url, data)
  },
}

export default {
  userApi,
  cardApi,
  adminCustomerApi,
  adminCardApi,
  adminTradeApi,
  adminDepositApi
} 